package com.xinya.wanandroidclient.ui.home

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.LinearLayoutManager
import com.xinya.wanandroidclient.R
import com.xinya.wanandroidclient.adapter.HomeArticleAdapter
import com.xinya.wanandroidclient.adapter.HomeBannerAdapter
import com.xinya.wanandroidclient.base.LazyBaseFragment
import com.xinya.wanandroidclient.bean.HomeArticleBean
import com.xinya.wanandroidclient.bean.HomeBannerBean
import com.xinya.wanandroidclient.databinding.FragmentHomeBinding
import com.xinya.wanandroidclient.http.LoadState
import com.xinya.wanandroidclient.view.NavHostFragment
import com.youth.banner.Banner
import com.youth.banner.indicator.RectangleIndicator

/**
 *
 * @ClassName:      HomeFragment$
 * @Description:    MainActivity -> MainFragment -> HomeFragment
 * @Author:         xinya
 * @CreateDate:     2021/2/23$ 19:36$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class HomeFragment : LazyBaseFragment() {
    companion object {
        private const val TAG = "HomeFragment"
    }

    private val homeViewModel by lazy {
        getFragmentVieModel(HomeViewModel::class.java)
    }

    private val articleAdapter by lazy {
        HomeArticleAdapter(null)
    }

    private val homeArticleListView by lazy {
        dataBinding.homeArticleList
    }

    private val homeSmartRefreshView by lazy {
        dataBinding.homeSmartRefresh
    }

    private val headView by lazy {
        LayoutInflater.from(context)
            .inflate(R.layout.item_banner, dataBinding.root as ViewGroup, false)
    }

    private val bannerView by lazy {
        headView.findViewById<Banner<HomeBannerBean, HomeBannerAdapter>>(R.id.banner)
    }

    private val bannerViewAdapter by lazy {
        HomeBannerAdapter(null)
    }

    private lateinit var dataBinding: FragmentHomeBinding

    override fun getLayoutId() = R.layout.fragment_home

    override fun initDataBinding(binding: ViewDataBinding?) {
        dataBinding = binding as FragmentHomeBinding
    }

    override fun observer() {
        homeViewModel.apply {
            // banner 数据监听
            bannerLiveData.observe(this@HomeFragment) {
                bannerView.setDatas(it)
            }
            // articleTop 数据监听
            articleTopLiveData.observe(this@HomeFragment) {
                articleAdapter.setNewInstance(it)
                homeViewModel.getHomeArticle(false)
            }
            // article 数据监听
            articleLiveData.observe(this@HomeFragment) {
                articleAdapter.addData(it.datas)
            }
            // errorLiveData 数据监听
            errorLiveData.observe(this@HomeFragment) {
                Toast.makeText(getContext(), it.errorMsg, Toast.LENGTH_SHORT).show()
            }
            // 数据刷新/加载 状态监听
            refreshStateLiveData.observe(this@HomeFragment) {
                when (it) {
                    LoadState.Error -> {
                        // smartRefresh数据加载失败
                        smartRefreshState(homeSmartRefreshView, false)
                        Toast.makeText(context, "网络异常", Toast.LENGTH_SHORT).show()
                    }
                    LoadState.Success -> {
                        // smartRefresh数据加载成功
                        smartRefreshState(homeSmartRefreshView, true)
                    }
                    else -> {
                    }
                }
            }
        }
    }

    override fun onClick() {
        dataBinding.etSearch.setOnClickListener {
            NavHostFragment.findNavController(this).navigate(R.id.action_main_to_search)
        }

        dataBinding.homeScan.setOnClickListener {
            // TODO: 2021/2/25 跳转到二维码识别
        }

        articleAdapter.setOnItemClickListener { adapter, _, position ->
            val item = adapter.getItem(position) as HomeArticleBean.Data
            item.isRead = true
            // 文章被点击查看一下  改变颜色
            articleAdapter.setData(position, item)
            NavHostFragment.findNavController(this)
                .navigate(R.id.action_main_to_web, Bundle().apply {
                    putString("title", item.title)
                    putInt("id", item.id)
                    putString("url", item.link)
                })
        }

        articleAdapter.setOnItemChildClickListener { adapter, view, position ->
            // TODO: 2021/2/25 item 里面点击 心心图标

        }

        bannerViewAdapter.setOnBannerListener { data, _ ->
            val homeBannerBean = data as HomeBannerBean
            NavHostFragment.findNavController(this)
                .navigate(R.id.action_main_to_web, Bundle().apply {
                    putString("title", homeBannerBean.title)
                    putInt("id", homeBannerBean.id)
                    putString("url", homeBannerBean.url)
                })
        }
    }

    override fun lazyInitView() {
        // banner 初始化
        bannerView.apply {
            adapter = bannerViewAdapter
            indicator = RectangleIndicator(context)
            setIndicatorNormalColorRes(R.color.white)
            setIndicatorSelectedColorRes(R.color.colorPrimary)
            addBannerLifecycleObserver(viewLifecycleOwner)
        }

        // smartRefresh 初始化
        homeSmartRefreshView.apply {
            // 自动刷新
            autoRefresh()
            // 使能加载更多
            setEnableLoadMore(true)
            // 使能重新加载
            setEnableRefresh(true)
            // 滚动回弹
            setEnableOverScrollDrag(true)
            // 重新加载监听
            setOnRefreshListener {
                homeViewModel.getHomeArticle(true)
                homeViewModel.getHomeBanner()
            }
            // 加载更多监听
            setOnLoadMoreListener {
                homeViewModel.getHomeArticle(false)
            }
        }

        // 文章 recyclerView初始化
        homeArticleListView.apply {
            layoutManager = LinearLayoutManager(context)
            adapter = articleAdapter.apply {
                addHeaderView(headView)
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "onCreate: ")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "onResume: ")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "onPause: ")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "onDestroy: a")
    }


}